package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.Part;
import com.pl.entity.SysUser;
import com.pl.service.PartService;
import com.pl.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * ----一夜暴富----
 *
 * @author mzy
 * @data 2024/5/5
 * @desc ----摸大腿不拍大腿----
 */

public class PartLister extends AnalysisEventListener<Part> {
    //定义初始加入的记录数
    private static final int BATCH_COUNT=5;
    //引入SysUserService,后边导入会用到
    private PartService partService;
    //创建初始集合存sysUser数据
    List<Part> partList=new ArrayList<>();

    public PartLister() {
    }

    public PartLister(PartService partService) {
        this.partService = partService;
    }

    /**
     * 每调用一次 会回调此方法,该方法规定一次插入5条数据
     * @param part
     * @param analysisContext
     */
    @Override
    public void invoke(Part part, AnalysisContext analysisContext) {
        partList.add(part);
        if (partList.size()>=BATCH_COUNT){
            saveDate();
            partList.clear();
        }
    }

    private void saveDate() {
        System.out.println("开始批量导入");
        partService.inserts(partList);
        System.out.println("结束批量导入");
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (partList.size()>0){//最后一次存的地方
            saveDate();
            partList.clear();
        }
    }
}
